<?php
require '../model/Db.php';
require '../model/Page.php';
$isMobile = preg_match('/mobile/i', $_SERVER['HTTP_USER_AGENT']);
$Db = DB::getInstance();
$user_data = $Db->islogin();
if(is_numeric($_GET['type']) && is_numeric($_GET['mid'])){
$path = PHOTO_PATH;
if(isset($_GET['mode'])){
    setcookie('mode',$_GET['mode'],time()+3600,'/nsbbs/');
}
switch ($_GET['type']) {
	case 0:
	    if($_COOKIE['mode'] == 1){
	        $mode1 = 'selected';
	        $mode2 = ''; 
	    }else{
	        $mode1 = '';
	        $mode2 = 'selected'; 
	    }
		$header = <<<header
				<div class="cont-bar clearfix">
                    <div class="all hand current">全部</div>
                    <div class="today hand">今日</div>
                    <div class="browse-more hand">By 浏览</div>
                    <select id="mode">
                        <option value="1" {$mode1}>主题帖</option>
                        <option value="0" {$mode2}>单帖</option>
                    </select>
                </div>
                <div class="cont-wrap">
header;
		$header_tt = <<<header_tt
                    <div class="list-title clearfix">
                        <div class="l num">序号</div>
                        <div class="l title">标题</div>
                        <div class="l author">作者</div>
                        <div class="l reply">回复</div>
                        <div class="l last-publish">最后发表</div>
                    </div>
header_tt;
			$footer = <<<footer
                    <div class="list-main">
                    </div>
                </div>
                <script>
                let mode = $('#mode');
                $.ajax({
		            url: 'ajax/module_page.php?type=3&mode='+(mode.val())+'&mid='+{$_GET['mid']},
		            type: 'GET',
		            dataType: 'html' 
		        })
		        .done(function(data) {
		            $('.list-main').html(data)
		        });
                //内标签页
				let con_bars = $('.cont-bar > div');
				con_bars.data('curr', 0);
				con_bars.click(function(event) {
				    let i = $(this).index();
				    if(con_bars.data('curr') === i){
				        return
				    }
				    $.ajax({
			            url: 'ajax/module_page.php?type='+(i+3)+'&mode='+(mode.val())+'&mid='+{$_GET['mid']},
			            type: 'GET',
			            dataType: 'html' 
			        })
			        .done(function(data) {
			            $('.list-main').html(data)
			        });
				    $(con_bars[con_bars.data('curr')]).removeClass('current');
				    $(this).addClass('current');
				    con_bars.data('curr',i)
				});
                //模式切换
                mode.change(function(){
                    $.ajax({
                        url: 'ajax/module_page.php?type='+(con_bars.data('curr')+3)+'&mode='+(mode.val())+'&mid='+{$_GET['mid']},
                        type: 'GET',
                        dataType: 'html' 
                    })
                    .done(function(data) {
                        $('.list-main').html(data)
                    });
                });
                </script>
footer;
	if(!$isMobile){
		$html = $header.$header_tt.$footer;
	}else{
		$html = $header.$footer;
	}
		
		break;
	
	case 1:
    $great_cnt = $Db->count([
        table => [content => 'a',module => 'b'],
        where => "a.mid = b.id and a.mid = {$_GET['mid']} and a.delete_time is NULL and a.great = 1 and a.cid = 0"
    ]);
    $page = page($great_cnt,PAGE_SIZE,PAGE_BTN_CNT);
    $great_data = $Db->find([
        table => [content => 'a',module => 'b',bbs_user => 'c'],
        field => 'a.*,c.user,c.id uid,c.photo',
        where => "a.mid = b.id and a.uid = c.id and a.delete_time is NULL and a.mid = {$_GET['mid']} and a.great = 1 and a.cid = 0 order by a.top,a.time desc {$page['limit']}"
    ]);
    $great_data = $Db->tohtml($great_data);
		$header = <<<header
                <div class="cont-wrap">
                    <div class="list-title clearfix">
                        <div class="l num">序号</div>
                        <div class="l title">标题</div>
                        <div class="l author">作者</div>
                        <div class="l reply">回复</div>
                        <div class="l last-publish">最后发表</div>
                    </div>
                    <div class="list-main">
header;
        $header_phone = <<<header_phone
                <div class="cont-wrap">
                    <div class="list-main">
header_phone;
    if(!$great_data){
        $body = '<div class="no-cont">还没有帖子纳入史诗帖哦~</div>';
    }else{
    if(!$isMobile){

    foreach ($great_data as $item) {
        $great_xuhao = $item['top']?'<div class="l num top"></div>':"<div class='l num'>{$item['id']}</div>";
        $last_push = $Db->find([
            table => [content => 'a',bbs_user => 'b'],
            field => 'b.user,a.time',
            where => "a.uid = b.id and a.cid = {$item['id']} and a.delete_time is NULL order by a.time desc limit 0,1"
        ])[0];
        $lastpush = '';
        if($last_push){
        $now_time = date_parse(date('Y-m-d H:i:s'));
        $last_time = date_parse($last_push['time']);
        if($now_time['year'] != $last_time['year']){//0102-03-04
            $last_push['time'] = substr($last_push['time'],0,10);
        }elseif($now_time['month'] != $last_time['month'] || $now_time['day']-$last_time['day'] > 2 ){ //01-02 03:04
            $last_push['time'] = substr($last_push['time'],5,-3);
        }elseif($now_time['day']-$last_time['day'] == 1){
            $last_push['time'] = '昨天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day']-$last_time['day'] == 2){
            $last_push['time'] = '前天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day'] == $last_time['day']){
            $diff = time()-date_timestamp_get(date_create($last_push['time']));
            if($diff < 3600){
                $last_push['time'] = round($diff/60).'分钟前';
            }else{
                $last_push['time'] = substr($last_push['time'],10,-3);
            }
        }
        $count = $Db->count([
            table => ['content'],
            where => "id = {$item['id']} or cid = {$item['id']} and delete_time is NULL"
        ]);
        $max_page = ceil($count/PAGE_SIZE);
        $last_push = $Db->tohtml($last_push);
        $lastpush = <<<lastpush
        <div class="l last-publish">
            <a href="details.php?cid={$item['id']}&page={$max_page}#end" class="link"></a>
            <div class="user-time">
                <div class="user">{$last_push['user']}</div>
                <div class="time">{$last_push['time']}</div>
            </div>
        </div>
lastpush;
    }
		$body .= <<<body
              
                        <div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                            {$great_xuhao}
                            <div class="l title">
                                {$item['title']}
                            </div>
                            <div class="l author clearfix">
                                <a target="_blank" href="userinfo.php?uid={$item['uid']}" class="link"></a>
                                <div class="l">
                                    <img class="img-yuan-x36" src="{$path}{$item['photo']}">
                                </div>
                                <div class="l user-time">
                                    <div class="user">{$item['user']}</div>
                                    <div class="time">{$item['time']}</div>
                                </div>
                            </div>
                            <div class="l reply">{$item['reply_num']}</div>
                            {$lastpush}
                        </div>

body;
    }
    }else{
        foreach ($great_data as $item) {
        $great_xuhao = $item['top']?'<span class="top"></span>':'';
        $body .= <<<body
              
                        <div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                            <div class="item-title">
                                {$great_xuhao}
                                {$item['title']}
                            </div>
                            <div class="l author-time">
                                <span class="author">{$item['user']}</span>
                                <span class="time">{$item['time']}</span>
                            </div>
                            <div class="r reply">
                                <b></b><span>{$item['reply_num']}</span>
                            </div>
                        </div> 

body;
    }
    }
}
		$footer = <<<footer
                    </div>
                    <ul class="pagination">
                    {$page['html']}
                    </ul>
                </div>
                <script>
                //分页
                let tab_content = $('.tab-content');
                let page_btn = $('.pagination a');
                page_btn.click(function(e){
                    e ? e.preventDefault() : window.e.returnValue = false;
                    $.ajax({
                        url: $(this).prop('href'),
                        type: 'GET',
                        dataType: 'html' 
                    })
                    .done(function(data) {
                        tab_content.html(data)
                    });
                });
                </script>
footer;
		if(!$isMobile){
			$html = $header.$body.$footer;
		}else{
			$html = $header_phone.$body.$footer;
		}
		
		break;

	case 2:
	$module = $Db->find([
		table => ['module'],
		where => "id = {$_GET['mid']}"
	]);
    $module = $Db->tohtml($module);
	$html = <<<html
	<div id="notice">
	<div class="title">公示</div>
	{$module[0]['notice']}
	</div>
html;
	break;
	case 3:
	//全部
    $where = $_GET['mode'] == 1 ? 'and a.cid = 0':''; 
    $all_cnt = $Db->count([
        table => [content => 'a',module => 'b'],
        where => "a.delete_time is NULL and a.mid = b.id and a.mid = {$_GET['mid']} {$where}"
    ]);
    $page = page($all_cnt,PAGE_SIZE,PAGE_BTN_CNT);
	$all_data = $Db->find([
		table => [content => 'a',module => 'b',bbs_user => 'c'],
		field => 'a.*,c.user,c.id uid,c.photo',
		where => "a.mid = b.id and a.uid = c.id and a.delete_time is NULL and a.mid = {$_GET['mid']} {$where} order by a.top,a.time desc {$page['limit']}"
	]);
    $all_data = $Db->tohtml($all_data);
	if(!$isMobile){
	foreach ($all_data as $item) {
		$all_xuhao = $item['top']?'<div class="l num top"></div>':"<div class='l num'>{$item['id']}</div>";
        $last_push = $Db->find([
            table => [content => 'a',bbs_user => 'b'],
            field => 'b.user,a.time',
            where => "a.uid = b.id and a.cid = {$item['id']} and a.delete_time is NULL order by a.time desc limit 0,1"
        ])[0];
        $lastpush = '';
        if($last_push){
        $now_time = date_parse(date('Y-m-d H:i:s'));
        $last_time = date_parse($last_push['time']);
        if($now_time['year'] != $last_time['year']){//0102-03-04
            $last_push['time'] = substr($last_push['time'],0,10);
        }elseif($now_time['month'] != $last_time['month'] || $now_time['day']-$last_time['day'] > 2 ){ //01-02 03:04
            $last_push['time'] = substr($last_push['time'],5,-3);
        }elseif($now_time['day']-$last_time['day'] == 1){
            $last_push['time'] = '昨天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day']-$last_time['day'] == 2){
            $last_push['time'] = '前天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day'] == $last_time['day']){
            $diff = time()-date_timestamp_get(date_create($last_push['time']));
            if($diff < 3600){
                $last_push['time'] = round($diff/60).'分钟前';
            }else{
                $last_push['time'] = substr($last_push['time'],10,-3);
            }
        }
        //获取总页数
        $count = $Db->count([
            table => ['content'],
            where => "id = {$item['id']} or cid = {$item['id']} and delete_time is NULL"
        ]);
        $max_page=ceil($count/PAGE_SIZE);
        $last_push = $Db->tohtml($last_push);
        $lastpush = <<<lastpush
        <div class="l last-publish">
            <a href="details.php?cid={$item['id']}&page={$max_page}#end" class="link"></a>
            <div class="user-time">
                <div class="user">{$last_push['user']}</div>
                <div class="time">{$last_push['time']}</div>
            </div>
        </div>
lastpush;
    }
	$html .= <<<html
						<div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                           	{$all_xuhao}
                            <div class="l title">
                                {$item['title']}
                            </div>
                            <div class="l author clearfix">
                                <a target="_blank" href="userinfo.php?uid={$item['uid']}" class="link"></a>
                                <div class="l">
                                    <img class="img-yuan-x36" src="{$path}{$item['photo']}">
                                </div>
                                <div class="l user-time">
                                    <div class="user">{$item['user']}</div>
                                    <div class="time">{$item['time']}</div>
                                </div>
                            </div>
                            <div class="l reply">{$item['reply_num']}</div>
                            {$lastpush}
                        </div>
html;
	}
}else{
	foreach ($all_data as $item) {
		$all_xuhao = $item['top']?'<span class="top"></span>':'';
	$html .= <<<html
						<div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                            <div class="item-title">
                                {$all_xuhao}
                                {$item['title']}
                            </div>
                            <div class="l author-time">
                                <span class="author">{$item['user']}</span>
                                <span class="time">{$item['time']}</span>
                            </div>
                            <div class="r reply">
                                <b></b><span>{$item['reply_num']}</span>
                            </div>
                        </div> 
html;
	}
}
        if(!$html){
            $html = <<<html
            <div class="no-cont">还没有发布过的帖子哦~</div>
html;
        }
        $page_btn = <<<page
                <ul class="pagination">
                    {$page['html']}
                </ul>
                <script>
                //分页
                let page_btn = $('.pagination a');
                page_btn.click(function(e){
                    e ? e.preventDefault() : window.e.returnValue = false;
                    $.ajax({
                        url: $(this).prop('href'),
                        type: 'GET',
                        dataType: 'html' 
                    })
                    .done(function(data) {
                        $('.list-main').html(data)
                    });
                });
                </script>
page;
        $html .= $page_btn;
		break;
	case 4:
	//今日
    $where = $_GET['mode'] ==1 ? 'and a.cid = 0':'';
    $today_cnt = $Db->count([
        table => [content => 'a',module => 'b'],
        where => "a.mid = b.id and a.delete_time is NULL and a.mid = {$_GET['mid']} and a.time > CURDATE() {$where}"
    ]);
    $page = page($today_cnt,PAGE_SIZE,PAGE_BTN_CNT);
	$today_data = $Db->find([
		table => [content => 'a',module => 'b',bbs_user => 'c'],
		field => 'a.*,c.user,c.id uid,c.photo',
		where => "a.mid = b.id and a.uid = c.id and a.delete_time is NULL and a.mid = {$_GET['mid']} and a.time > CURDATE() {$where} order by a.top,a.time desc {$page['limit']}"
	]);
    $today_data = $Db->tohtml($today_data);
	if(!$isMobile){
	foreach ($today_data as $item) {
		$today_xuhao = $item['top']?'<div class="l num top"></div>':"<div class='l num'>{$item['id']}</div>";
        $last_push = $Db->find([
            table => [content => 'a',bbs_user => 'b'],
            field => 'b.user,a.time',
            where => "a.uid = b.id and a.cid = {$item['id']} and a.delete_time is NULL order by a.time desc limit 0,1"
        ])[0];
        $lastpush = '';
        if($last_push){
        $now_time = date_parse(date('Y-m-d H:i:s'));
        $last_time = date_parse($last_push['time']);
        if($now_time['year'] != $last_time['year']){//0102-03-04
            $last_push['time'] = substr($last_push['time'],0,10);
        }elseif($now_time['month'] != $last_time['month'] || $now_time['day']-$last_time['day'] > 2 ){ //01-02 03:04
            $last_push['time'] = substr($last_push['time'],5,-3);
        }elseif($now_time['day']-$last_time['day'] == 1){
            $last_push['time'] = '昨天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day']-$last_time['day'] == 2){
            $last_push['time'] = '前天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day'] == $last_time['day']){
            $diff = time()-date_timestamp_get(date_create($last_push['time']));
            if($diff < 3600){
                $last_push['time'] = round($diff/60).'分钟前';
            }else{
                $last_push['time'] = substr($last_push['time'],10,-3);
            }
        }
        $count = $Db->count([
            table => ['content'],
            where => "id = {$item['id']} or cid = {$item['id']} and delete_time is NULL"
        ]);
        $max_page = ceil($count/PAGE_SIZE);
        $last_push = $Db->tohtml($last_push);
        $lastpush = <<<lastpush
        <div class="l last-publish">
            <a href="details.php?cid={$item['id']}&page={$max_page}#end" class="link"></a>
            <div class="user-time">
                <div class="user">{$last_push['user']}</div>
                <div class="time">{$last_push['time']}</div>
            </div>
        </div>
lastpush;
    }
	$html .= <<<html
						<div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                            {$today_xuhao}
                            <div class="l title">
                                {$item['title']}
                            </div>
                            <div class="l author clearfix">
                                <a target="_blank" href="userinfo.php?uid={$item['uid']}" class="link"></a>
                                <div class="l">
                                    <img class="img-yuan-x36" src="{$path}{$item['photo']}">
                                </div>
                                <div class="l user-time">
                                    <div class="user">{$item['user']}</div>
                                    <div class="time">{$item['time']}</div>
                                </div>
                            </div>
                            <div class="l reply">{$item['reply_num']}</div>
                            {$lastpush}
                        </div>
html;
	}
}else{
	foreach ($today_data as $item) {
		$today_xuhao = $item['top']?'<span class="top"></span>':'';
	$html .= <<<html
						<div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                            <div class="item-title">
                                {$today_xuhao}
                                {$item['title']}
                            </div>
                            <div class="l author-time">
                                <span class="author">{$item['user']}</span>
                                <span class="time">{$item['time']}</span>
                            </div>
                            <div class="r reply">
                                <b></b><span>{$item['reply_num']}</span>
                            </div>
                        </div> 
html;
	}
}
        if(!$html){
            $html = <<<html
            <div class="no-cont">今日还没有发布的帖子哦~</div>
html;
        }
        $page_btn = <<<page
                <ul class="pagination">
                    {$page['html']}
                </ul>
                <script>
                //分页
                let page_btn = $('.pagination a');
                page_btn.click(function(e){
                    e ? e.preventDefault() : window.e.returnValue = false;
                    $.ajax({
                        url: $(this).prop('href'),
                        type: 'GET',
                        dataType: 'html' 
                    })
                    .done(function(data) {
                        $('.list-main').html(data)
                    });
                });
                </script>
page;
        $html .= $page_btn;
		break;
	case 5:
	//浏览
    $where = $_GET['mode'] ==1 ? 'and a.cid = 0':'';
    $look_cnt = $Db->count([
        table => [content => 'a',module => 'b'],
        where => "a.mid = b.id and a.delete_time is NULL and a.mid = {$_GET['mid']} {$where}"
    ]);
    $page = page($look_cnt,PAGE_SIZE,PAGE_BTN_CNT);
	$look_data = $Db->find([
		table => [content => 'a',module => 'b',bbs_user => 'c'],
		field => 'a.*,c.user,c.id uid,c.photo',
		where => "a.mid = b.id and a.uid = c.id and a.delete_time is NULL and a.mid = {$_GET['mid']} {$where} order by a.top,a.look_num desc,a.time desc {$page['limit']}"
	]);
    $look_data = $Db->tohtml($look_data);
	if(!$isMobile){
	foreach ($look_data as $item) {
		$look_xuhao = $item['top']?'<div class="l num top"></div>':"<div class='l num'>{$item['id']}</div>";
        $last_push = $Db->find([
            table => [content => 'a',bbs_user => 'b'],
            field => 'b.user,a.time',
            where => "a.uid = b.id and a.cid = {$item['id']} and a.delete_time is NULL order by a.time desc limit 0,1"
        ])[0];
        $lastpush = '';
        if($last_push){
        $now_time = date_parse(date('Y-m-d H:i:s'));
        $last_time = date_parse($last_push['time']);
    
        if($now_time['year'] != $last_time['year']){//0102-03-04
            $last_push['time'] = substr($last_push['time'],0,10);
        }elseif($now_time['month'] != $last_time['month'] || $now_time['day']-$last_time['day'] > 2 ){ //01-02 03:04
            $last_push['time'] = substr($last_push['time'],5,-3);
        }elseif($now_time['day']-$last_time['day'] == 1){
            $last_push['time'] = '昨天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day']-$last_time['day'] == 2){
            $last_push['time'] = '前天'.substr($last_push['time'],10,-3);
        }elseif($now_time['day'] == $last_time['day']){
            $diff = time()-date_timestamp_get(date_create($last_push['time']));
            if($diff < 3600){
                $last_push['time'] = round($diff/60).'分钟前';
            }else{
                $last_push['time'] = substr($last_push['time'],10,-3);
            }
        }
        $count = $Db->count([
            table => ['content'],
            where => "id = {$item['id']} or cid = {$item['id']} and delete_time is NULL"
        ]);
        $max_page = ceil($count/PAGE_SIZE);
        $last_push = $Db->tohtml($last_push);
        $lastpush = <<<lastpush
        <div class="l last-publish">
            <a href="details.php?cid={$item['id']}&page={$max_page}#end" class="link"></a>
            <div class="user-time">
                <div class="user">{$last_push['user']}</div>
                <div class="time">{$last_push['time']}</div>
            </div>
        </div>
lastpush;
    }
	$html .= <<<html
						<div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                            {$look_xuhao}
                            <div class="l title">
                                {$item['title']}
                            </div>
                            <div class="l author clearfix">
                                <a target="_blank" href="userinfo.php?uid={$item['uid']}" class="link"></a>
                                <div class="l">
                                    <img class="img-yuan-x36" src="{$path}{$item['photo']}">
                                </div>
                                <div class="l user-time">
                                    <div class="user">{$item['user']}</div>
                                    <div class="time">{$item['time']}</div>
                                </div>
                            </div>
                            <div class="l reply">{$item['reply_num']}</div>
                            {$lastpush}
                        </div>
html;
	}
}else{
	foreach ($look_data as $item) {
		$look_xuhao = $item['top']?'<span class="top"></span>':'';
	$html .= <<<html
						<div class="list-item hand clearfix">
                            <a href="details.php?cid={$item['id']}" class="link"></a>
                            <div class="item-title">
                                {$look_xuhao}
                                {$item['title']}
                            </div>
                            <div class="l author-time">
                                <span class="author">{$item['user']}</span>
                                <span class="time">{$item['time']}</span>
                            </div>
                            <div class="r reply">
                                <b></b><span>{$item['reply_num']}</span>
                            </div>
                        </div> 
html;
	}
}
        if(!$html){
            $html = <<<html
           <div class="no-cont">还没有发布过的帖子哦~</div>
html;
        }
        $page_btn = <<<page
                <ul class="pagination">
                    {$page['html']}
                </ul>
                <script>
                //分页
                let page_btn = $('.pagination a');
                page_btn.click(function(e){
                    e ? e.preventDefault() : window.e.returnValue = false;
                    $.ajax({
                        url: $(this).prop('href'),
                        type: 'GET',
                        dataType: 'html' 
                    })
                    .done(function(data) {
                        $('.list-main').html(data)
                    });
                });
                </script>
page;
        $html .= $page_btn;
		break;
}

}
if($html){
	echo $html;
}else{
	http_response_code(404);
	echo '<h1 style="color:#337ab7">千呼万唤始出来,犹抱琵琶半遮面</h1>';
}
$Db->close();